﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using Kaiser.Infrastructure.Controllers;
using Kaiser.Models.JsonModels;
using Kaiser.Services;

namespace Kaiser.Modules.Admin.Controllers
{
    public class DeptManageController:KaiserBaseController
    {
        private readonly ISystemManageService _systemManageService;

        public DeptManageController(ISystemManageService systemManageService)
        {
            _systemManageService = systemManageService;
        }

        #region 部门管理
        public ActionResult DeptList()
        {
            return View();
        }

        public ActionResult GetDeptTree(string parentId)
        {
           //var result = _systemManageService.GetSyncOrgTree(parentId);

            var node = new JsonTreeNode();
            node.id = "100001";
            node.text = "海豚工作室";
            node.children=new List<JsonTreeNode>();
            node.children.Add(new JsonTreeNode()
                {
                    id = "100002",
                    text="CMS小组"
                });
            node.children.Add(new JsonTreeNode()
            {
                id = "100003",
                text = "供应商小组"
            });
            node.children.Add(new JsonTreeNode()
            {
                id = "100004",
                text = "工作流小组"
            });

            var json = new JavaScriptSerializer();
            var result= json.Serialize(new JsonTreeNode[] { node });

            return Content(result, "text/html");
        }

        public ActionResult EditDept(string id)
        {
            Kaiser.Models.Org model;
            if (string.IsNullOrWhiteSpace(id))
            {
                model = new Kaiser.Models.Org();
            }
            else
            {
                model = _systemManageService.GetOrg(id);
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult EditDept(string id, Kaiser.Models.Org model)
        {
            var result = new JsonReturnMessages();

            try
            {

                if (string.IsNullOrEmpty(id))
                {
                    model.CreateDateTime = DateTime.Now;
                    model.CreateUserFullName = CurrentUser.DisplayName;
                    model.CreateUserName = CurrentUser.Name;
                 
                    _systemManageService.InsertOrg(model);
                }
                else
                {
                    model.LastUpdateTime = DateTime.Now;
                    model.LastUpdateUserFullName = CurrentUser.DisplayName;
                    model.LastUpdateUserName = CurrentUser.Name;
                    _systemManageService.UpdateOrg(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }

 
        public ActionResult AddDept()
        {
            return View("EditDept", new Kaiser.Models.Org());
        }

        [HttpPost]
        public ActionResult AddDept(Kaiser.Models.Org model)
        {
            return EditDept("", model);
        }

        #endregion
    }

  
}
